package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javabean.Role;
import com.javabean.Users;
import com.service.RoleService;
import com.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/users")
public class UsersController {
    @Autowired
    UsersService usersService;
    @Autowired
    RoleService roleService;

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(HttpServletRequest request){
        Object users = request.getSession().getAttribute("users");
        if(users!=null){
            return "main";
        }
        return "login";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(HttpServletRequest request,String username, String password){
        Users login = usersService.login(username, password);
        if(login!=null){
            request.getSession().setAttribute("users", login);
            return "main";
        }else{
            request.setAttribute("msg", "账号或密码错误！");
            return "/login";
        }
    }
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().invalidate();
        return "/login";
    }

    @RequestMapping("/findAll")
    public String findAll(HttpServletRequest request, @RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "limit",defaultValue = "5")  int limit){
        PageHelper.startPage(page,limit);
        List<Users> all = usersService.findAll();
        PageInfo<Users> pageInfo=new PageInfo<>(all);
        request.setAttribute("pageInfo", pageInfo);
        return "/users/users-list";
    }
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String toAdd(HttpServletRequest request){

        List<Role> roleList = roleService.findAll();
        request.setAttribute("roleList",roleList);
        return "/users/users-add";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(Users users,int roleId){
        usersService.add(users,roleId);
        return "redirect:/users/findAll";
    }
    @RequestMapping(value = "edit",method = RequestMethod.GET)
    public String edit(HttpServletRequest request,int id){
        Users users = usersService.findById(id);
        List<Role> all = roleService.findAll();
        request.setAttribute("roleList", all);
        request.setAttribute("users", users);
        return "/users/users-edit";
    }

    @RequestMapping(value = "edit",method = RequestMethod.POST)
    public String edit(Users users,int roleId){
        usersService.edit(users, roleId);
        return "redirect:findAll";
    }
}
